package com.aivin.maproutelib.line;
import com.aivin.maproutelib.bean.WaypointInfo;

/**
 * 用来在生成航点时逻辑上需要。
 */
public class ScanLine {

    /**
     * y = k * x + b
     * 线的K值，斜率
     */
    public double k;
    /**
     * 得到线的 b 值
     */
    public double b;
    /**
     * y = k * x + b ， b 的第一次值
     */
    private final double bInited;


    private double maxX = Double.MAX_VALUE;
    private double minX = Double.MIN_VALUE;

    public ScanLine(WaypointInfo start, float direction) {
        if (direction % 90 == 0) {
            direction = direction + 0.0001f;
        }
        // 弧度=角度*Math.PI/180
        k = Math.tan( direction *  Math.PI / 180 );
        b = start.getLongitude() - k * start.getLatitude();

        bInited = b;
    }

    public ScanLine(WaypointInfo start, double k) {
        this.k = k;
        b = start.getLongitude() - k * start.getLatitude();

        bInited = b;
    }

    public ScanLine(WaypointInfo startPoint, WaypointInfo endPoint) {
        double deltaX = endPoint.getLatitude() - startPoint.getLatitude();
        double deltaY = endPoint.getLongitude() - startPoint.getLongitude();
        if (deltaY == 0) {
            deltaY += 0.0000001f;
        }
        k = deltaY / deltaX;
        b = startPoint.getLongitude() - k * startPoint.getLatitude();
        bInited = b;

        minX = Math.min(startPoint.getLatitude(), endPoint.getLatitude());
        maxX = Math.max(startPoint.getLatitude(), endPoint.getLatitude());
    }


    /**
     * y = k * x + b  ,获取Y值
     */
    public double getY(double x) {
        return (k * x + b);
    }

    /**
     * y = k * x + b  , 增加 b
     */
    public void addDaltaB(double detalB) {
        b += detalB;
    }


    /**
     * y = k * x + b  , 重置 b
     */
    public void reset() {
        b = bInited;
    }

    /**
     * 指定的x值是否在两点之间的 x 区间内 ,
     * 这条线是由这两个点确定 。
     */
    public boolean isInXRange(double x) {
        return x >= minX && x <= maxX;
    }


}
